package com.hy.demo.app;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.hy.demo.R;
import com.hy.frame.common.ILoadingDialog;
import com.hy.frame.common.ILoadingUI;
import com.hy.frame.ui.LoadingDialog;
import com.hy.frame.ui.LoadingUI;
import com.hy.frame.ui.ToolbarUI;
import com.hy.frame.util.LogUtil;

/**
 * title BaseFragment
 * author HeYan
 * time 2021/2/3 11:22
 * desc 无
 */
public abstract class BaseFragment extends com.hy.frame.base.BaseFragment {
    private ToolbarUI mToolbarUI;
    private ILoadingDialog mLoadingDialog;
    private ILoadingUI mLoadingUI;
    private View mLayoutView;

    protected boolean isSingleLayout() {
        return true;
    }

    protected int getLayoutId() {
        return 0;
    }

    protected boolean isTranslucentStatus(){
        if (getActivity() instanceof BaseActivity){
            return  ((BaseActivity)getActivity()).isTranslucentStatus();
        }
        return false;
    }

    private LifecycleEventObserver mEventObserver;

    @Nullable
    @Override
    protected View getRootLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mLayoutView != null) return mLayoutView;
        if (getContext() == null) return null;
        if (getLayoutId() == 0) return null;
        if (isSingleLayout()) {
            mLayoutView = inflater.inflate(getLayoutId(),null);
        } else {
            mLayoutView = inflater.inflate(R.layout.v_base,null);
            FrameLayout cMain = mLayoutView.findViewById(R.id.kf_main);
            if (cMain != null)
                View.inflate(getContext(), getLayoutId(), cMain);
        }
        Toolbar toolbar = mLayoutView.findViewById(R.id.kf_toolbar);
        if (toolbar != null) {
            mToolbarUI = new ToolbarUI(getContext()).setTranslucentStatus(isTranslucentStatus()).build((ViewGroup) toolbar.getParent());
            mToolbarUI.setListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onViewClick(v);
                }
            });
        }
        if (mEventObserver == null) {
            mEventObserver = new LifecycleEventObserver() {
                @Override
                public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                    LogUtil.d(BaseFragment.this.getClass(), "event=" + event);
                }
            };
            getLifecycle().addObserver(mEventObserver);
        }
        return mLayoutView;
    }
    public ToolbarUI getToolbarUI() {
        return this.mToolbarUI;
    }

    public ILoadingUI getLoadingUI() {
        if (mLoadingUI == null) {
            FrameLayout cMain = findViewById(R.id.kf_main);
            if (cMain != null) {
                mLoadingUI = new LoadingUI(cMain);
                mLoadingUI.setErrorClick(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onLoadViewClick();
                    }
                });
            }
        }
        return mLoadingUI;
    }

    public ILoadingDialog getLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getContext());
        }
        return mLoadingDialog;
    }

    public void setTitle(CharSequence title) {
        if (getToolbarUI() != null)
            getToolbarUI().setTitle(title);
    }

    protected void onLeftClick() {

    }

    protected void onRightClick() {

    }

    protected void onLoadViewClick() {

    }

    protected void onViewClick(View v) {

    }

}
